/*
* author: wancheng
* date: 11/5/18
* desc:
*/


export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) time = parseInt(time) * 1000;
    time = +time; // 转成int 型
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}


export function clone(obj) {
  var result = '';
  //
  if (typeof obj === 'object') {
    try {
      result = JSON.stringify(obj);
      result = JSON.parse(result);
    } catch (e) {
      result = obj;
    }
  } else {
    result = obj;
  }

  return result;
}

export function extend(target) {
  var args = Array.prototype.slice.call(arguments, 1);

  for (var i = 0, len = args.length; i < len; i++) {
    var source = args[i];
    for (var key in source) {
      if (source[key] !== undefined) {
        target[key] = source[key];
      }
    }
  }
  return target;
}



export function uuid() {
// return (+new Date() * 1e6 + Math.floor(Math.random() * 1e6)).toString(36);
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0;
    var v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}


export function isIos() {
  var ua = window.navigator.userAgent;
  return !!ua.match(/iphone|ipad|iPod/gi);
};

export function isAndroid() {
  var ua = window.navigator.userAgent;
  return !!ua.match(/android/gi);
}

export function isH5() {
  return isAndroid() || isIos();
}
